<template>
    <div>
        <h1> 欢迎你:{{ userName }} 来到个人中心！ 这里只有登录过后才能看见</h1>
    </div>
</template>


<script>
export default {
    name: 'MyCenter',
    data() {
        return {
            fromUrl: ''
        }
    },
    computed: {
        isLogin() {
            return this.$store.state.isLogin
        },
        userName() {
            return this.$store.state.userName
        }
    },
    watch: {
        userName(newVal, oldVal) {
            this.$router.push(this.$route.params.fromUrl)
        }
    },

    // 进入组件前的 组件内部路由守卫
    beforeRouteEnter(to, from, next) {
        const _isLogin = JSON.parse(localStorage.getItem('isLogin'))
        // this.fromUrl = from.path
        if (_isLogin) {
            next()
        } else {
            next('/login')
        }
    },

}

</script>